/**************************************************************/
#ifndef __SPIX_h__
#define __SPIX_h__

#include "cm32m101a.h"
#include "cm32m101a_gpio.h"
#include "cm32m101a_conf.h"
#include "cm32m101a_spi.h"


#define  SPI_RX_BUFFER_SIZE       10
#define  SPI_TX_BUFFER_SIZE       10

//#define SPI_MASTER                SPI1
//#define SPI_MASTER_CLK            RCC_APB2_PERIPH_SPI1
//#define SPI_MASTER_GPIO           GPIOA
//#define SPI_MASTER_GPIO_CLK       RCC_APB2_PERIPH_GPIOA
//#define SPI_MASTER_PIN_NSS        GPIO_PIN_4
//#define SPI_MASTER_PIN_SCK        GPIO_PIN_5
//#define SPI_MASTER_PIN_MISO       GPIO_PIN_6
//#define SPI_MASTER_PIN_MOSI       GPIO_PIN_7

//#define SPI_MASTER_Rx_DMA_Channel DMA_CH3
//#define SPI_MASTER_Rx_DMA_FLAG    DMA_FLAG_TC3
//#define SPI_MASTER_Rx_DMA_REMAP   DMA_REMAP_SPI1_RX
//#define SPI_MASTER_Tx_DMA_Channel DMA_CH4
//#define SPI_MASTER_Tx_DMA_FLAG    DMA_FLAG_TC4
//#define SPI_MASTER_Tx_DMA_REMAP   DMA_REMAP_SPI1_TX
//#define SPI_MASTER_DR_Base        SPI1_BASE+0x0c //0x4001300C

//#define SPI_SLAVE                SPI2
//#define SPI_SLAVE_CLK            RCC_APB2_PERIPH_SPI2
//#define SPI_SLAVE_GPIO           GPIOB
//#define SPI_SLAVE_GPIO_CLK       RCC_APB2_PERIPH_GPIOB
//#define SPI_SLAVE_PIN_NSS        GPIO_PIN_12
//#define SPI_SLAVE_PIN_SCK        GPIO_PIN_13
//#define SPI_SLAVE_PIN_MISO       GPIO_PIN_14
//#define SPI_SLAVE_PIN_MOSI       GPIO_PIN_15

//#define SPI_SLAVE_Rx_DMA_Channel DMA_CH1
//#define SPI_SLAVE_Rx_DMA_FLAG    DMA_FLAG_TC1
//#define SPI_SLAVE_Rx_DMA_REMAP   DMA_REMAP_SPI2_RX
//#define SPI_SLAVE_Tx_DMA_Channel DMA_CH2
//#define SPI_SLAVE_Tx_DMA_FLAG    DMA_FLAG_TC2
//#define SPI_SLAVE_Tx_DMA_REMAP   DMA_REMAP_SPI2_TX
//#define SPI_SLAVE_DR_Base        SPI2_BASE+0x0c //0x40013C0C

#define SPI_MASTER                SPI2
#define SPI_MASTER_CLK            RCC_APB2_PERIPH_SPI2
#define SPI_MASTER_GPIO           GPIOB
#define SPI_MASTER_GPIO_CLK       RCC_APB2_PERIPH_GPIOB
#define SPI_MASTER_PIN_NSS        GPIO_PIN_12
#define SPI_MASTER_PIN_SCK        GPIO_PIN_13
#define SPI_MASTER_PIN_MISO       GPIO_PIN_14
#define SPI_MASTER_PIN_MOSI       GPIO_PIN_15
                                    
#define SPI_MASTER_Rx_DMA_Channel DMA_CH1
#define SPI_MASTER_Rx_DMA_FLAG    DMA_FLAG_TC1
#define SPI_MASTER_Rx_DMA_REMAP   DMA_REMAP_SPI2_RX
#define SPI_MASTER_Tx_DMA_Channel DMA_CH2
#define SPI_MASTER_Tx_DMA_FLAG    DMA_FLAG_TC2
#define SPI_MASTER_Tx_DMA_REMAP   DMA_REMAP_SPI2_TX
#define SPI_MASTER_DR_Base        SPI2_BASE+0x0c        //0x40013C0C

typedef struct SPI_buff{
    
    uint8_t  RX_Buff[SPI_RX_BUFFER_SIZE];
    uint8_t  TX_Buff[SPI_TX_BUFFER_SIZE];
    
    uint8_t  RX_Time;
    uint8_t  RX_Flg;
    uint16_t TX_Cnt;
    uint16_t RX_Cnt;
    
}spi_buff_TypeDef;  

extern spi_buff_TypeDef  spi_buff;

void SPI_Configuration(void);

void SPIx_Transmit_DMA(SPI_Module* SPIx,uint8_t* buff,uint16_t buff_size );

#endif  //__SPIX_h__


